`timescale 1ns / 1ps

module status_machine(
	input clk_i,  // 1kHz
	input rst_i,
	input start0_i,
    input start1_i,
    input start2_i,
    input [1:0] success,
	
	(*KEEP="true"*)output reg [2:0] status,
	(*KEEP="true"*)output reg [2:0] substatus
	);
	
	reg [9:0] timestamp;
	
	// We may need this to enjoy a smooth head start
	initial begin
		status <= 'd0;
		substatus <= 'd7;
		timestamp <= 'd0;
	end
	
	always @(posedge clk_i or posedge rst_i) begin
        if (rst_i) begin
        	status <= 'd0;
        	substatus <= 'd7;
        	timestamp <= 'd0;
        end else begin
			// Update timestamp
			// As you see, the period of the timestamp is 1Hz
            timestamp <= (timestamp==999) ? 0 : timestamp+1;
			
			// update status and substatus
		    case (status)
		        3'd1: begin
		            status <= (substatus==3'd5) ? 3'd2 : 3'd1;
					// Update substatus every 1 second
					// In order to show the 5 random numbers
		            case (substatus)
		                3'd5: substatus <= substatus;
		                default: substatus <= timestamp ? substatus : substatus+1;
		            endcase
		        end
				3'd0: status <= (~rst_i & start0_i) ? 3'd1 : 3'd0;
		        3'd2: status <= start1_i ? 3'd3 : 3'd2;
		        3'd3: status <= (success[1] & start2_i) ? 'd4 : 'd3;
		        3'd4: status <= success[0] ? 'd5 : 'd4;
		        3'd5: status <= status;
		        default: status <= 3'd0;	
			endcase
		end
    end
endmodule

